#include "widgets.h"
#include "callbacks.h"
#include "glade_xml.h"
#include "string.h"

void init_widgets() {
	xml = glade_xml_new_from_buffer(glade_xml_string, strlen(glade_xml_string), NULL, NULL);
	//printf("%s\n",g_get_);
	glade_xml_signal_autoconnect(xml);
	mainWindow = glade_xml_get_widget(xml, "mainWindow");
	mainStatusBar = (GtkStatusbar *)glade_xml_get_widget(xml, "mainStatusBar");
	userView = (GtkTreeView *)glade_xml_get_widget(xml, "userView");
	create_view_and_model(GTK_WIDGET(userView));
	
	chatWindow = glade_xml_get_widget(xml, "chatWindow");
	incomingText = (GtkTextView *)glade_xml_get_widget(xml, "incomingText");
	outgoingText = (GtkTextView *)glade_xml_get_widget(xml, "outgoingText");

	gtk_widget_show(mainWindow);
}

void add_user(gchar * name, gchar * service, gchar * ip) {
	gtk_list_store_append (userStore, &userIter);
	gtk_list_store_set (userStore, &userIter,
			COL_NAME, name,
			COL_SERVICE, service,
			COL_IP, ip,
			-1);
}

void remove_user(gchar * service) {
	gtk_tree_model_foreach(GTK_TREE_MODEL(userStore), remove_foreach_func, (gpointer)service);
}

gboolean remove_foreach_func(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
{
	gchar *name, *service, *tree_path_str, *ip;
	gboolean ret = FALSE;

	/* Note: here we use 'iter' and not '&iter', because we did not allocate
	 *  the iter on the stack and are already getting the pointer to a tree iter */

	gtk_tree_model_get(model, iter, COL_NAME, &name, COL_SERVICE, &service, COL_IP, &ip, -1);

	tree_path_str = gtk_tree_path_to_string(path);
	
	if(g_ascii_strcasecmp(service, (gchar *)user_data) == 0) {
		gtk_list_store_remove(userStore, iter);
		ret = TRUE;
	}

	//g_print("Row %s: %s %s, %s\n", tree_path_str, name, service, ip);

	g_free(tree_path_str);

	g_free(name); /* gtk_tree_model_get made copies of       */
	g_free(service); /* the strings for us when retrieving them */
	g_free(ip);

	return FALSE; /* do not stop walking the store, call us with next row */
}

gboolean get_user_from_ip_foreach_func(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
{
	gchar *name, *service, *tree_path_str, *ip;
	gboolean ret;
	user_ip_t * userIp = (user_ip_t *)user_data;
	/* Note: here we use 'iter' and not '&iter', because we did not allocate
	 *  the iter on the stack and are already getting the pointer to a tree iter */

	gtk_tree_model_get(model, iter, COL_NAME, &name, COL_SERVICE, &service, COL_IP, &ip, -1);

	tree_path_str = gtk_tree_path_to_string(path);
	
	if(g_ascii_strcasecmp(ip, userIp->ip) == 0) {
		//gtk_list_store_remove(userStore, iter);
		ret = TRUE;
		userIp->user = name;
	}

	//g_print("Row %s: %s %s, %s\n", tree_path_str, name, service, ip);

	g_free(tree_path_str);

	g_free(name); /* gtk_tree_model_get made copies of       */
	g_free(service); /* the strings for us when retrieving them */
	g_free(ip);

	return FALSE; /* do not stop walking the store, call us with next row */
}

GtkTreeModel * create_and_fill_model(void) {
	userStore = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

	return GTK_TREE_MODEL (userStore);
}

GtkWidget * create_view_and_model(GtkWidget *view) {
	GtkCellRenderer *renderer;
	GtkTreeModel *model;
	//GtkWidget           *view;

	//view = gtk_tree_view_new ();

	/* --- Column #1 --- */

	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW (view),
	-1,
	"Name",
	renderer,
	"text", COL_NAME,
	NULL);

	model = create_and_fill_model();

	gtk_tree_view_set_model(GTK_TREE_VIEW (view), model);

	/* The tree view has acquired its own reference to the
	 *  model, so we can drop ours. That way the model will
	 *  be freed automatically when the tree view is destroyed */

	g_object_unref(model);

	return view;
}

void add_message(gchar * message, gchar * user) {
	gtk_text_buffer_insert_at_cursor(incomingText->buffer,
					g_strconcat(user, ": ", message, NULL),
					-1);
	printf("(CALLBACK) void add_message(gchar * message, gchar * user)\n");
}

gchar * get_user_from_ip(gchar * ip) {
	user_ip_t * userIp;
	userIp->ip = ip;
	gtk_tree_model_foreach(GTK_TREE_MODEL(userStore), get_user_from_ip_foreach_func, (gpointer)userIp);
	return (gchar *)userIp->user;
}
